package com.flex.reflection.convert;

import com.flex.reflection.MetaClass;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单类型转换
 * @author ganlt
 * @since 2019/09/23 14:05
 */
public class SimpleConvertType {
    private Map<Class,FieldConvert> converts = new HashMap<>();

    public SimpleConvertType(MetaClass.Config config){
        converts.put(Integer.class,new FieldIntegerConvert(config));
        converts.put(int.class,new FieldIntegerConvert(config));
        converts.put(String.class,new FieldStringConvert(config));
        converts.put(Date.class,new FieldDateConvert(config));
    }

    public Object convert(Object param,Class<?> clazz) {
        if (param == null || clazz == null) {
            return null;
        }

        FieldConvert fieldConvert = converts.get(clazz);
        if (fieldConvert != null) {
            return fieldConvert.convert(param);
        }

        return null;
    }
}
